package com.mljr.example.ui.home.tab1;

import android.app.Activity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.ctakit.sdk.app.utils.DensityUtil;
import com.ctakit.sdk.app.widget.autoscroller.AutoScrollViewPager;
import com.ctakit.sdk.view.ViewInjector;
import com.ctakit.sdk.view.annotation.ViewInject;
import com.mljr.example.R;
import com.mljr.example.adapter.tab1.HeaderAdAdapter;
import com.mljr.example.bean.tab1.ChannelIcon;

import java.util.ArrayList;
import java.util.List;


public class ChannelCustomViewPager extends HeaderViewInterface<List<ChannelIcon>> {

    @ViewInject(R.id.vp_ad)
    AutoScrollViewPager vpAd;
    @ViewInject(R.id.ll_index_container)
    LinearLayout llIndexContainer;

    private List<View> ivList;



    public ChannelCustomViewPager(Activity context) {
        super(context);
        ivList = new ArrayList<>();
    }

    @Override
    protected void getView(List<ChannelIcon> list, ListView listView) {
        View view = mInflate.inflate(R.layout.header_channel_veiw_pager_layout, listView, false);
        ViewInjector.inject(this, view);

        dealWithTheView(list);
        listView.addHeaderView(view);
    }

    private void dealWithTheView(List<ChannelIcon> list) {
        int size = list.size();
        if(size<=8){
            // 设置频道数据
            HeaderChannelView headerChannelView = new HeaderChannelView(mContext);
            headerChannelView.fillView(list, ivList);
        }else if(size>8&&size<=16){

            for(int i=0 ;i<2;i++){
                // 设置频道数据
                HeaderChannelView headerChannelView = new HeaderChannelView(mContext);
                headerChannelView.fillView(list.subList(0,8), ivList);
                // 设置频道数据
                HeaderChannelView headerChannelView2 = new HeaderChannelView(mContext);
                headerChannelView2.fillView(list.subList(8,16), ivList);
            }

        }

        HeaderAdAdapter photoAdapter = new HeaderAdAdapter(mContext, ivList);
        vpAd.setAdapter(photoAdapter);
        vpAd.setCurrentItem(ivList.size()*100);

        addIndicatorImageViews(ivList.size()/2);
        setViewPagerChangeListener(ivList.size()/2);
       // startADRotate();
    }


    // 添加指示图标
    private void addIndicatorImageViews(int size) {
        llIndexContainer.removeAllViews();
        for (int i = 0; i < size; i++) {
            ImageView iv = new ImageView(mContext);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(DensityUtil.dip2px(5), DensityUtil.dip2px(5));
            if (i != 0) {
                lp.leftMargin = DensityUtil.dip2px(7);
            }
            iv.setLayoutParams(lp);
            iv.setBackgroundResource(R.drawable.view_pager_dot_theme_grey_sel);
            iv.setEnabled(false);
            if (i == 0) {
                iv.setEnabled(true);
            }
            llIndexContainer.addView(iv);
        }
    }

    // 为ViewPager设置监听器
    private void setViewPagerChangeListener(final int size) {
        vpAd.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                if (ivList != null && ivList.size() > 0) {
                    int newPosition = position % size;
                    for (int i = 0; i < size; i++) {
                        llIndexContainer.getChildAt(i).setEnabled(false);
                        if (i == newPosition) {
                            llIndexContainer.getChildAt(i).setEnabled(true);
                        }
                    }
                }
            }

            @Override
            public void onPageScrolled(int position, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }



    // 停止循环广告的线程，清空消息队列
    public void stopADRotate() {
        vpAd.startAutoScroll();
    }

}
